<template>
	<view class="content">
	    <u-cell-group>
	        <u-cell  v-for="item in sourceList" :key="item.id">
	            <view slot="title" class="u-slot-title">
	                <view style="">
	                    {{ item.name }}
	                </view>
	            </view>
	            <view slot="label" class="u-slot-label">
	                <view class="btn">
	                    <u-button type="primary" :customStyle="btnStyle" size="mini" text="修改"
	                        @click="toshow(item)"></u-button>
	                    <u-button type="error" :customStyle="btnStyle" size="mini" text="删除"
	                        @click="getdeldata(item, 'error')">
	                    </u-button>
	                </view>
	            </view>
	        </u-cell>
	    </u-cell-group>
	    <u-modal :show="show" :content='content' :showCancelButton="true" @confirm="confirm(content)"
	        @cancel="show = false">
			<u--input
			    placeholder="请输入客户来源"
			    border="surround"
			    v-model="content.name"
			  ></u--input>
			</u-modal>
			<u-modal :show="show1" :content='content' :showCancelButton="true" @confirm="confirm1(content)"
			    @cancel="show1 = false">
				<u--text align='center' text="您确定删除该客户来源吗？"></u--text>
				</u-modal> 
	    <u-toast ref="uToast"></u-toast>
		<u-button @click="add" :customStyle="btnStyle1" color="#19BC9C" type="success">添加客户来源</u-button>
	</view>
</template>

<script>
	export default{
		data() {
		    return {
				btnStyle1: {
				    width: '100%',
				    position: 'fixed',
				    bottom: '0px'
				},
				show:false,
				show1:false,
				content:{
					name:''
				},
				btnStyle: {
				    width: '10%',
				    display: "inline-block",
				    marginRight: '5px',
				    marginTop: '10px',
				    lineHeight: '20px'
				},
		        LoginUser: {},
				sourceList:[],
				
		    }
		},
		onLoad() {
		    this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {};
		},
		methods:{
			add(){
				this.show=true
			},
			async getSourceData(){
				let data = {
				    id:this.LoginUser.id,
				    openid:this.LoginUser.openid
				}
				
				let result = await this.$u.api.source.getSource(data)
				if(result.code==1){
					this.sourceList=result.data
				}else{
					this.$refs.uToast.show({
					    type:'error',
					    message:result.msg
					})
					
					return false
				}
			},
			toshow(e){
				this.show=true
				this.content=e
			},
			async confirm(i){
				let _this=this;
				if(i.id==undefined){
					let data = {
					    id:this.LoginUser.id,
						name:this.content.name
					}
					let result = await this.$u.api.source.addSource(data)
					if(result.code==1){
						this.show=false;
						this.$refs.uToast.show({
						    type:'success',
						    message:result.msg,
							complete(){
								_this.content={};
								_this.getSourceData();
							}
						})
						
						return false
					}else{
						this.show=false;
						this.$refs.uToast.show({
						    type:'error',
						    message:result.msg,
							complete(){
								_this.content={};
							}
						})
						
						return false
					}
					
				}else{
					let data = {
					    id:this.LoginUser.id,
						name:this.content.name,
						sourceid:i.id
					}
					let result = await this.$u.api.source.editSource(data)
					if(result.code==1){
						this.show=false;
						this.$refs.uToast.show({
						    type:'success',
						    message:result.msg,
							complete(){
								_this.content={};
								_this.getSourceData();
							}
						})
						
						return false
					}else{
						this.show=false;
						this.$refs.uToast.show({
						    type:'error',
						    message:result.msg,
							complete(){
								_this.content={};
							}
						})
						
						return false
					}
				}

			},
			getdeldata(e){
				this.show1=true;
				this.content=e;
			},
			async confirm1(i){
				let _this=this;
				let data = {
				    id:this.LoginUser.id,
					sourceid:i.id
				}
				let result = await this.$u.api.source.delSource(data)
				if(result.code==1){
					this.show1=false;
					this.$refs.uToast.show({
					    type:'success',
					    message:result.msg,
						complete(){
							_this.content={};
							_this.getSourceData();
						}
					})
					
					return false
				}else{
					this.show1=false;
					this.$refs.uToast.show({
					    type:'error',
					    message:result.msg,
						complete(){
							_this.content={};
						}
					})
					
					return false
				}
				}
		},
		onShow(){
			this.getSourceData()
		},
	}
</script>

<style>
</style>